
import CryptoJS from 'crypto-js'
import AES from 'crypto-js/aes'
import Base64 from 'crypto-js/enc-base64'
import MD5 from 'crypto-js/md5'
import SHA1 from 'crypto-js/sha1'

var requestConfig = {
    // encryptOpen: 0,
	encryptOpen: 1,
    AESKey: 'OGFiMGFhM2UwOGI5Y2E0Yw=='
}

var needCheckList = ['/common/uploadImage/']


export default {
   
	// 加密
	encript(config) {
		// debugger;
		if (config.url) {
			if (config.data && requestConfig.encryptOpen && needCheckList.indexOf(config.url) == -1) {
				// console.log('---加密前的入参----：', config.url, config.data)
				var jsonData = JSON.stringify(config.data)
				let aesPrivateKey = requestConfig.AESKey
				var key = CryptoJS.enc.Utf8.parse(aesPrivateKey);
				var srcs = CryptoJS.enc.Utf8.parse(jsonData);
				var encrypted = CryptoJS.AES.encrypt(srcs, key, {
					mode: CryptoJS.mode.ECB,
					padding: CryptoJS.pad.Pkcs7
				});
				//最终请求发送的终极加密参数
				config.data = {
					data: encrypted.toString()
				}
			}
			return config;
		} else {
			console.error('url不能为空~')
		}
	},
	// 解密
	decript(res) {
		// console.log('需要解密的参数', res)
		if (requestConfig.encryptOpen && res.data.data && needCheckList.indexOf(res.config.url) == -1) {
			// console.log('需要解密的参数', res)
			let resData = res.data.data;
			let aesPrivateKey = requestConfig.AESKey
			var key = CryptoJS.enc.Utf8.parse(aesPrivateKey);
			let decryptedbytes = CryptoJS.AES.decrypt(resData, key, {
				mode: CryptoJS.mode.ECB,
				padding: CryptoJS.pad.Pkcs7
			});
			var jsonData = CryptoJS.enc.Utf8.stringify(decryptedbytes).toString()
			// console.log('需要解密的结果', jsonData)
			res.data.data = JSON.parse(jsonData)
			return res
		} else {
			return res
		}
	}
};
